package diamondServer.businesslogic.router;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import diamondServer.businesslogic.resource.DefaultResRepository;
import diamondServer.businesslogic.resource.Resource;
import diamondServer.businesslogic.resource.ResourcesRepository;
import diamondServer.resources.MessageFake;
import diamondServer.resources.ServiceFake;


import junit.framework.TestCase;

public class DefaultRouterTest
		extends TestCase
{
	private ResourcesRepository resRepository;
	private Router router;
	
	private final ByteArrayOutputStream out =
		new ByteArrayOutputStream(); //for syso testing
	private final String LINE_SEPARATOR =
		System.getProperty("line.separator");
	
	protected void setUp() throws Exception
	{
		resRepository = DefaultResRepository.getInstance();
		router = new DefaultRouter(resRepository);
		System.setOut(new PrintStream(out));
	}

	protected void tearDown() throws Exception
	{
		resRepository.removeAll();
		System.setOut(null); 
	}

	public void testRoute() 
		throws RouterException
	{
		MessageFake msg = new MessageFake();
		Resource resource = new Resource(msg, new ServiceFake());
		resRepository.addResource("ResourceFake", resource);
		router.route(msg);
		
		assertEquals(new MessageFake().getBody()+LINE_SEPARATOR, 
				out.toString());
	}

}
